﻿/*
    Copyright (c) 2007 - 2009, Carlos Guzmán Álvarez

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
    are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright notice, 
          this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright notice, 
          this list of conditions and the following disclaimer in the documentation and/or 
          other materials provided with the distribution.
        * Neither the name of the author nor the names of its contributors may be used to endorse or 
          promote products derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using Framework.Net.Xmpp.Serialization.InstantMessaging.Client;

namespace Framework.Net.Xmpp.Core
{
    /// <summary>
    /// Chat message
    /// </summary>
    public sealed class XmppMessage
    {
        #region · Fields ·

        private string  identifier;
        private XmppJid from;
        private string  subject;
        private string  body;
        private string  thread;
        private string  language;
        private bool    isActive;
        private bool    isComposing;
        private bool    isGone;
        private bool    isInactive;
        private bool    isPaused;

        #endregion

        #region · Properties ·

        /// <summary>
        /// Gets the message identifier
        /// </summary>
        public string Identifier
        {
            get { return this.identifier; }
        }

        /// <summary>
        /// Gets the source of the message.
        /// </summary>
        /// <value>From.</value>
        public XmppJid From
        {
            get { return this.from; }
        }

        /// <summary>
        /// Gets the subject of the message.
        /// </summary>
        /// <value>The subject.</value>
        public string Subject
        {
            get { return this.subject; }
        }

        /// <summary>
        /// Gets the body of the message.
        /// </summary>
        /// <value>The body.</value>
        public string Body
        {
            get { return this.body; }
        }

        /// <summary>
        /// Gets the thread of the message.
        /// </summary>
        /// <value>The thread.</value>
        public string Thread
        {
            get { return this.thread; }
        }

        /// <summary>
        /// Gets the language.
        /// </summary>
        /// <value>The language.</value>
        public string Language
        {
            get { return this.language; }
        }

        /// <summary>
        /// Gests a value indicating is the user is active in the conversation
        /// </summary>
        public bool IsActive
        {
            get { return isActive; }
        }

        /// <summary>
        /// Gests a value indicating is the user is composing a message
        /// </summary>
        public bool IsComposing
        {
            get { return isComposing; }
            set { isComposing = value; }
        }

        /// <summary>
        /// Gests a value indicating is the user is gone from the conversation
        /// </summary>
        public bool IsGone
        {
            get { return isGone; }
            set { isGone = value; }
        }

        /// <summary>
        /// Gests a value indicating is the user is inactive from the conversation
        /// </summary>
        public bool IsInactive
        {
            get { return isInactive; }
            set { isInactive = value; }
        }

        /// <summary>
        /// Gests a value indicating is the user is paused in the conversation
        /// </summary>
        public bool IsPaused
        {
            get { return this.isPaused; }
        }

        #endregion

        #region · Constructors ·

        /// <summary>
        /// Initializes a new instance of the <see cref="T:XmppIncomingChatMessageEventArgs"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        internal XmppMessage(Message message)
        {
            this.Initialize(message);
        }

        #endregion

        #region · Private methods ·

        private void Initialize(Message message)
        {
            this.identifier = message.ID;
            this.from       = new XmppJid(message.From);
            this.language   = message.Lang;
            this.thread     = "";

            foreach (object item in message.Items)
            {
                if (item is MessageBody)
                {
                    this.body = ((MessageBody)item).Value;
                }
                else if (item is MessageSubject)
                {
                    this.subject = ((MessageSubject)item).Value;
                }
                else if (item is NotificationActive)
                {
                    this.isActive = true;
                }
                else if (item is NotificationComposing)
                {
                    this.isComposing = true;
                }
                else if (item is NotificationGone)
                {
                    this.isGone = true;
                }
                else if (item is NotificationInactive)
                {
                    this.isInactive = true;
                }
                else if (item is NotificationPaused)
                {
                    this.isPaused = true;
                }
            }
        }

        #endregion
    }
}
